﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL.Entity;
using DAL.Persistencia;

namespace NewTierra.Pages
{
    public partial class VerTarifasPorPeriodo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            //Session["PeriodoIDTarifasVer"] = 10;
            ViewState["TipoTarifa"] = "";
            CarregarDadosTarifas(sender, e);
            CarregarDadosMeal(sender, e);
            CarregarDadosPeriodos(sender, e);
            }
        }

        #region Popular Periodos

        protected void CarregarDadosPeriodos(object sender, EventArgs e)
        {
            CarregarPeriodos(sender, e);
        }

        protected void CarregarPeriodos(object sender, EventArgs e)
        {
            try
            {
                PeriodosDAL p = new PeriodosDAL();

                Dictionary<string, string> dadosPeriodos = (Dictionary<string, string>)Session["IdsPeriodosVer"];

                GridTarifasPeriodo.DataSource = p.ListarTodos(Convert.ToInt32(Session["Sid"]), Convert.ToInt32(dadosPeriodos["IdMercado"]), Convert.ToInt32(dadosPeriodos["IdBaseTarifaria"]));
                GridTarifasPeriodo.DataBind();

                if (GridTarifasPeriodo.PageCount > 0)
                {
                    int i = 0;

                    foreach (GridViewRow row in GridTarifasPeriodo.Rows)
                    {
                        if (row.Cells[0].Text == Session["PeriodoIDTarifasVer"].ToString())
                        {
                            GridTarifasPeriodo.SelectedIndex = i;
                            break;
                        }

                        i++;
                    }
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void GridTarifasPeriodo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["PeriodoIDTarifasVer"] = GridTarifasPeriodo.SelectedRow.Cells[0].Text;
            CarregarDadosTarifas(sender, e);
            CarregarDadosMeal(sender, e);
        }

        #endregion

        #region Carrega Dados Tarifas
        
        protected void CarregarDadosTarifas(object sender, EventArgs e)
        {
            CarregarTarifas(sender, e);
        }

        protected void CarregarTarifas(object sender, EventArgs e)
        {
            try
            {
                TarifaDAL t = new TarifaDAL();

                if (ViewState["TipoTarifa"].Equals("Apartamento"))
                {
                    GridTarifas.DataSource = t.ListarTodos(Convert.ToInt32(Session["PeriodoIDTarifasVer"]),"Multiplica");
                    GridTarifas.DataBind(); //Desenhar
                }
                else
                {
                    GridTarifas.DataSource = t.ListarTodos(Convert.ToInt32(Session["PeriodoIDTarifasVer"]));
                    GridTarifas.DataBind(); //Desenhar
                }

                MergeCells();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void PaginarDadosTarifas(object sender, GridViewPageEventArgs e)
        {
            //Realiza a paginação
            GridTarifas.PageIndex = e.NewPageIndex;
            CarregarDadosTarifas(sender, e); //Atualizar os dados do GridView
        }

        protected void RdoTarifa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RdoTarifa.SelectedValue.Equals("1"))
            {
                ViewState["TipoTarifa"] = "Apartamento";
                CarregarDadosTarifas(sender, e);
            }
            else
            {
                ViewState["TipoTarifa"] = "";
                CarregarDadosTarifas(sender, e);
            }
        }

        private void MergeCells()
        {
            int i = GridTarifas.Rows.Count - 2;
            while (i >= 0)
            {
                GridViewRow curRow = GridTarifas.Rows[i];
                GridViewRow preRow = GridTarifas.Rows[i + 1];

                int j = 0;
                while (j < curRow.Cells.Count)
                {
                    if (curRow.Cells[j].Text == preRow.Cells[j].Text)
                    {
                        if (preRow.Cells[j].RowSpan < 2)
                        {
                            curRow.Cells[j].RowSpan = 2;
                            preRow.Cells[j].Visible = false;
                        }
                        else
                        {
                            curRow.Cells[j].RowSpan = preRow.Cells[j].RowSpan + 1;
                            preRow.Cells[j].Visible = false;
                        }
                    }
                    j++;
                }
                i--;
            }
        }

        #endregion

        #region Carrega Dados Meal

        protected void CarregarDadosMeal(object sender, EventArgs e)
        {
            CarregarMeal(sender, e);
        }

        protected void CarregarMeal(object sender, EventArgs e)
        {
            try
            {
                MealDAL m = new MealDAL();

                GridMeal.DataSource = m.ListarTodos(Convert.ToInt32(Session["PeriodoIDTarifasVer"]));
                GridMeal.DataBind(); //Desenhar
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void PaginarDadosMeal(object sender, GridViewPageEventArgs e)
        {
            //Realiza a paginação
            GridMeal.PageIndex = e.NewPageIndex;
            CarregarDadosMeal(sender, e); //Atualizar os dados do GridView
        }

        #endregion


    }
}